/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Control;
import DataBase.ServicioMenu;
import Logic.Clasificacion;
import Logic.Columna;
import Logic.Log;
import Logic.Tabla;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LiN
 */
public class GUI_ClasificationCataloge extends javax.swing.JPanel {
    private Control control;
    /**
     * Creates new form GUI_ClasificationCataloge
     */
    public GUI_ClasificationCataloge() {
        initComponents();
        control=Control.getInstance();
        //Bordes
        TitledBorder title1=BorderFactory.createTitledBorder("Categories");
        jPanel1.setBorder(title1);
        TitledBorder title2=BorderFactory.createTitledBorder("Privileges");
        jPanel2.setBorder(title2);        
        
        //Tablas
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        this.jTable1.getTableHeader().setReorderingAllowed(false);
        this.jTable2.getTableHeader().setReorderingAllowed(false);
        
        loadTable();
    }
    
    public void clearTable(JTable jtable){
        while(jtable.getRowCount()>0){
            ((DefaultTableModel)jtable.getModel()).removeRow(0);
        }
    }
    
    public void loadTable(){
        clearTable(this.jTable1);
        clearTable(this.jTable2);
        Iterator ite=control.getClasificaciones().iterator();
        while(ite.hasNext()){
            Clasificacion aux=(Clasificacion) ite.next();
            ((DefaultTableModel)this.jTable1.getModel()).addRow(new Object[]{aux.getNombreRol(),aux.getDescripcion()});
        }
    }
    
    public void loadTable2(){
        if(this.jTable1.getSelectedRowCount()>0){
        Clasificacion aux=control.getClasificacion((String)((DefaultTableModel)this.jTable1.getModel()).getValueAt(this.jTable1.getSelectedRow(),0));
        control.setSELECTED_CLASIFICACION(aux);
            Tabla auxTabla;
            Columna auxCol;
            clearTable(jTable2);
            //System.out.println(aux.getClasificacion()+aux.getDescripcion()+aux.getNombreRol());
            for(int i=0;i<aux.getTablaPermisos().size();i++){
                auxTabla=aux.getTablaPermisos().get(i);
                ((DefaultTableModel) jTable2.getModel()).addRow(new Object[]{auxTabla.getNombre(),"",auxTabla.isInsert(),auxTabla.isDelete(),auxTabla.isAlter(),auxTabla.isSelect(),false});
                for(int x=0;x<auxTabla.getColumans().size();x++){
                    auxCol=auxTabla.getColumans().get(x);
                    //Columnas
                    ((DefaultTableModel) jTable2.getModel()).addRow(new Object[]{auxTabla.getNombre(),auxCol.getNombre(),auxCol.isInsert(),false,auxCol.isUpdate(),false});
                }
            }
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton6 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(889, 32767));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Categories"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Category", "Description"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTable1MouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Update");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("New");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(115, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Privileges"));
        jPanel2.setMaximumSize(new java.awt.Dimension(889, 32767));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Table_Name", "Colum_Name", "Insert", "Delete", "Update", "Select"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(2).setPreferredWidth(30);
        jTable2.getColumnModel().getColumn(3).setPreferredWidth(30);
        jTable2.getColumnModel().getColumn(4).setPreferredWidth(30);
        jTable2.getColumnModel().getColumn(5).setPreferredWidth(30);

        jButton6.setText("Assing Privilege");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton6)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton6)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(this.jTable1.getSelectedRowCount()>0){
            new GUI_NewClasification((String)((DefaultTableModel)this.jTable1.getModel()).getValueAt(this.jTable1.getSelectedRow(),0),(String)((DefaultTableModel)this.jTable1.getModel()).getValueAt(this.jTable1.getSelectedRow(),1),this);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if(!control.getSELECTED_SERVER().equals(""))
            new GUI_NewClasification(this);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(this.jTable1.getSelectedRowCount()>0){
            int val=JOptionPane.showConfirmDialog(null,"Are you sure?","Confirm",JOptionPane.YES_NO_OPTION);
            if(val==0){
                Clasificacion cla =  control.getClasificacion((String)((DefaultTableModel)this.jTable1.getModel()).getValueAt(this.jTable1.getSelectedRow(),0));
                control.getClasificaciones().remove(control.getClasificacion((String)((DefaultTableModel)this.jTable1.getModel()).getValueAt(this.jTable1.getSelectedRow(),0)));
                ServicioMenu su=control.getServicioMenu();
                try {
                    su.dropRole(control.getSELECTED_CLASIFICACION().getNombreRol());
                    
                    //Logs
                    Log log = new Log(new Date(),control.getUser(),control.getSELECTED_SERVER(),Log.DELETE_CLASIFICATION,cla.getClasificacion()+"/"+cla.getDescripcion());
                    control.insertLog(log);
                    control.saveLogs();
                } catch (    SQLException | ClassNotFoundException ex) {
                    Logger.getLogger(GUI_ClasificationCataloge.class.getName()).log(Level.SEVERE, null, ex);
                }
                loadTable();
                control.save_clasification();
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseReleased
        this.loadTable2();
    }//GEN-LAST:event_jTable1MouseReleased

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if(this.jTable1.getSelectedRowCount()>0)
            new GUI_Permisos(this);
        else
            JOptionPane.showMessageDialog(null,"No Clasification Selected...");
    }//GEN-LAST:event_jButton6ActionPerformed




    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
